<div id="photo"></div>

<%= spinner %>

<% unless product.photos.blank? %>

  <h2>Hlavní fotka</h2>

  <% photo = product.photos.first %>
  <%= link_to (image_tag photo.attachment.url(:small), :style => "float: left; margin: 0px 15px 5px 0px;"), photo.attachment.url(:large), :rel => "prettyPhoto#{product.photos.count > 1 ? '[product]' : ''}" %>

  <br style="clear: left;" />

  <% permitted_to? :delete, :admin_product_photos do %>
    <%= button_to_delete "Smazat fotku", admin_product_product_photo_path(product.id, photo.id) %>
  <% end %>
  <% permitted_to? :edit, :admin_product_photos do %>
    <%= button_to_edit "Editovat fotku", edit_admin_product_product_photo_path(product.id, photo.id) %>
  <% end %>

  <%#*<div>%>
    <%# product.variants.each do |variant| %>
        <%#= variant.parameter_values_to_sentence %><br />
    <%# end %>
  <%#*</div>%>

  <% product.photos.shift %>

  <% unless product.photos.blank? %>
    <h3 style="clear: left;">Ostatní fotky</h3>
  
    <table class="table">
      <thead>
        <tr>
          <th class="first" style="width: 205px;"><%= "#{t 'helpers.label.photo.view'}" %></th>
          <% if product.photos.length > 2 %>
          <th><%= "#{t 'helpers.label.photo.position'}" %></th>
          <% end %>
          <th><%= "#{t 'helpers.label.photo.variant'}" %></th>
          <th style="width: 120px;"><%= "#{t 'helpers.label.photo.description'}" %></th>
          <th class="last" style="<%= product.photos.length > 2 ? "width: 100px;" : "" %>"></th>
        </tr>
      </thead>
      <tfoot>
        <tr>
          <th><%= remote_button_to_new "Přidat fotku", new_admin_product_product_photo_path, "photo" %></th>
        </tr>
      </tfoot>
      <tbody>
        <% product.photos.each_with_index do |photo, index| %>
        <tr>
          <td><%= link_to (image_tag photo.attachment.url(:thumb)), photo.attachment.url(:large), :rel => "prettyPhoto[product]" %></td>
          <% if product.photos.length > 2 %>
          <td><%= index %></td>
          <% end %>
          <td>
            <% product.variants.each do |variant| %>
              <% if variant.photos.include?(photo) %>
                <%= variant.parameter_values_to_sentence %>
              <% end %>
            <% end %>
          </td>
          <td><%= photo.description.blank? ? "Není vyplněn" : photo.description %></td>
          <td>
            <% permitted_to? :edit, :admin_product_photos do %>
              <% if product.photos.length > 2 %>
                <%= button_to_top "Posuonut fotku nahoru", move_admin_product_product_photo_path(product.id, photo.id, :method => "move_higher") %>
              <% end %>
            <% end %>
            <% permitted_to? :delete, :admin_product_photos do %>
              <%= button_to_delete "Smazat fotku", admin_product_product_photo_path(product.id, photo.id) %>
            <% end %>
            <% permitted_to? :edit, :admin_product_photos do %>
              <%= button_to_edit "Editovat fotku", edit_admin_product_product_photo_path(product.id, photo.id) %>
              <% if product.photos.length > 2 %>
                <%= button_to_bottom "Posunout fotku dolu", move_admin_product_product_photo_path(product.id, photo.id, :method => "move_lower") %>
              <% end %>
            <% end %>
          </td>
        </tr>
        <% end %>
      </tbody>
    </table>
  <% else %>
    <%= remote_button_to_new "Přidat fotku", new_admin_product_product_photo_path, "photo" %>
  <% end %>
<% else %>
  <p>K tomuto výrobku ještě nejsou přiřazeny žádné fotky.</p>

  <%= remote_button_to_new "Přidat fotku", new_admin_product_product_photo_path, "photo" %>
<% end %>

<script type="text/javascript">
  jQuery(function() {
    var toggleSpinner = function() {$("#spinner").toggle()};

    $(".photo")
      .bind("ajax:beforeSend", toggleSpinner)
      .bind("ajax:complete", toggleSpinner);
  });
</script>